package com.example.day07_tab;

import android.util.SparseArray;

import androidx.fragment.app.Fragment;

public class FragFactory {
    public static final int FRAGMENT_HOME = 1;//代表首页fragment---BlankFragment
    public static final int FRAGMENT_RECOMMEND = 2;//代表推荐界面
    public static final int FRAGMENT_ATTENTION = 3;//代表关注界面
    public static final int FRAGMENT_COMPUTER = 4;//代表电脑
    public static final int FRAGMENT_PHONE = 5;//代表手机

    //稀疏数组  只能存入不同的值
    public static SparseArray<Fragment> list = new SparseArray<>();

    //创建fragment
    public static Fragment createFg(int name){
        Fragment fragment = list.get(name);
        if(fragment==null){
            switch (name){
                case FRAGMENT_HOME:
                    fragment = new BlankFragment();
                    break;
                case FRAGMENT_RECOMMEND:
                    fragment = new BlankFragment2();
                    break;
                case FRAGMENT_ATTENTION:
                    fragment = new BlankFragment3();
                    break;
                case FRAGMENT_COMPUTER:
                    fragment = new BlankFragment4();
                    break;
                case FRAGMENT_PHONE:
                    fragment = new BlankFragment5();
                    break;
            }
            list.put(name,fragment);//无论创建的那个fragment,添加到数据中
        }
        return fragment;
    }

}
